package org.yu.team2023.flashdays.client.render;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.ModLoader;
import org.lwjgl.opengl.GL11;
import org.yu.team2023.flashdays.client.DamageParticleEvent;
import org.yu.team2023.flashdays.net.DamageParticlePackage;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@OnlyIn(Dist.CLIENT)
public class DamageParticlesManager {
    public static HashSet<DamageParticlePackage> PARTICLES = new HashSet<>();

    public static void tick() {
        PARTICLES.forEach(DamageParticlePackage::tick);
        PARTICLES.removeIf(DamageParticlePackage::camRemoved);
    }

    public static void render(PoseStack matrix) {
        PARTICLES.forEach(particle -> {
            renderParticle(matrix, particle);
        });
    }

    private static void renderParticle(PoseStack matrix, DamageParticlePackage particle) {
        Camera camera=Minecraft.getInstance().getEntityRenderDispatcher().camera;

        Minecraft client = Minecraft.getInstance();
        //玩家收到的伤害在自己视角下不渲染，除非selfShow==true
        if (client.player != null && particle.uuid.equals(client.player.getUUID()) && !particle.selfShow)
            return;


        float scaleToGui = Mth.lerp(particle.getScale(), 0.05f, 0.025f);

        var camPos = camera.getPosition();

        Vec3 cameraLocation = client.gameRenderer.getMainCamera().getPosition();
        Vec3 offset = cameraLocation.subtract(particle.x, particle.y,particle.z).normalize();
        double x = particle.x+offset.x;
        double y = particle.y+offset.y;
        double z = particle.z+offset.z;


        double camX = camPos.x;
        double camY = camPos.y;
        double camZ = camPos.z;

        matrix.pushPose();
        matrix.translate(x - camX, y - camY, z - camZ);
        matrix.mulPose(Axis.YP.rotationDegrees(-camera.getYRot()));
        matrix.mulPose(Axis.XP.rotationDegrees(camera.getXRot()));
        matrix.scale(-scaleToGui, -scaleToGui, scaleToGui);

        RenderSystem.setShader(GameRenderer::getPositionColorShader);
        RenderSystem.enableDepthTest();
        RenderSystem.enableBlend();
        RenderSystem.blendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE,
                GL11.GL_ZERO);
        drawDamageNumber(matrix, particle, 0, 0, 10);

        RenderSystem.disableBlend();

        matrix.popPose();
    }

    public static void drawDamageNumber(PoseStack matrix, DamageParticlePackage particle, double x, double y,
                                        float width) {

        Minecraft minecraft = Minecraft.getInstance();
        DamageParticleEvent damageParticleEvent = new DamageParticleEvent(particle);
        int sw = minecraft.font.width(damageParticleEvent.getText());
        ModLoader.get().postEvent(damageParticleEvent);
        int color = damageParticleEvent.getColor();
        minecraft.font.drawInBatch(damageParticleEvent.getText(), (int) (x + (width / 2) - sw), (int) y + 5, color, false,matrix.last().pose(), minecraft.renderBuffers().bufferSource(), Font.DisplayMode.NORMAL, 0, 15728880);
    }
}
